home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50a Issue 142 (CD142a) (August 1998).iso / full / jbuild / setup / JBuilder / jsamples.z / misc.jar / sunw / demo / misc / TickTock.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-06-04  |  1.8 KB  |  68 lines

  1. package sunw.demo.misc;
  2.  
  3. import java.beans.PropertyChangeListener;
  4. import java.beans.PropertyChangeSupport;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import java.io.Serializable;
  8. import java.util.Date;
  9.  
  10. public class TickTock implements Runnable, Serializable {
  11.    private PropertyChangeSupport changes = new PropertyChangeSupport(this);
  12.    private int interval = 5;
  13.    transient Thread runner;
  14.  
  15.    public TickTock() {
  16.       this.reset();
  17.    }
  18.  
  19.    private void reset() {
  20.       this.runner = new Thread(this);
  21.       this.runner.start();
  22.    }
  23.  
  24.    public int getSeconds() {
  25.       return (int)((new Date()).getTime() / 1000L);
  26.    }
  27.  
  28.    public int getInterval() {
  29.       return this.interval;
  30.    }
  31.  
  32.    public void setInterval(int var1) {
  33.       this.interval = var1;
  34.       if (this.runner != null) {
  35.          this.runner.interrupt();
  36.       }
  37.  
  38.    }
  39.  
  40.    public void run() {
  41.       int var1 = this.getSeconds();
  42.  
  43.       while(true) {
  44.          try {
  45.             Thread.sleep((long)(this.interval * 1000));
  46.          } catch (InterruptedException var3) {
  47.          }
  48.  
  49.          int var2 = this.getSeconds();
  50.          this.changes.firePropertyChange("seconds", new Integer(var1), new Integer(var2));
  51.          var1 = var2;
  52.       }
  53.    }
  54.  
  55.    public void addPropertyChangeListener(PropertyChangeListener var1) {
  56.       this.changes.addPropertyChangeListener(var1);
  57.    }
  58.  
  59.    public void removePropertyChangeListener(PropertyChangeListener var1) {
  60.       this.changes.removePropertyChangeListener(var1);
  61.    }
  62.  
  63.    private void readObject(ObjectInputStream var1) throws ClassNotFoundException, IOException {
  64.       var1.defaultReadObject();
  65.       this.reset();
  66.    }
  67. }
  68.